package com.vanke.vms.management.dao.daoImpl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.vanke.vms.common.domain.AddCtrl;
import com.vanke.vms.management.dao.ManageActivityDao;

public class ManageActivityDaoImpl extends HibernateDaoSupport implements
		ManageActivityDao {
	public List<List<String>> loadActivityList() {
		List<List<String>> allList = new ArrayList<List<String>>();
		List<String> list1 = new ArrayList<String>();
		List<String> list2 = new ArrayList<String>();
		List<String> list3 = new ArrayList<String>();
		List<String> list4 = new ArrayList<String>();
		List<String> list5 = new ArrayList<String>();
		List<String> list6 = new ArrayList<String>();
		List<AddCtrl> addCtrlList = (List<AddCtrl>) getHibernateTemplate()
				.find("from AddCtrl as adc order by adc.pname");
		for (int i = 0; i < addCtrlList.size(); i++) {
			if (addCtrlList.get(i).getPname().equals("交付阶段")) {
				list1.add(addCtrlList.get(i).getCname());
			} else if (addCtrlList.get(i).getPname().equals("入住阶段")) {
				list2.add(addCtrlList.get(i).getCname());
			} else if (addCtrlList.get(i).getPname().equals("房屋保修")) {
				list3.add(addCtrlList.get(i).getCname());
			} else if (addCtrlList.get(i).getPname().equals("投诉")) {
				list4.add(addCtrlList.get(i).getCname());
			} else if (addCtrlList.get(i).getPname().equals("咨询")) {
				list5.add(addCtrlList.get(i).getCname());
			} else if (addCtrlList.get(i).getPname().equals("满意度回访")) {
				list6.add(addCtrlList.get(i).getCname());
			}
		}
		allList.add(list1);
		allList.add(list2);
		allList.add(list3);
		allList.add(list4);
		allList.add(list5);
		allList.add(list6);
		return allList;
	}

	public void removeActivity(String pname, String cname) {
		SessionFactory sessionfactory = getHibernateTemplate()
				.getSessionFactory();
		Session session = sessionfactory.openSession();
		String sql = "delete from vms.addinfo where pname = :pname and cname = :cname";
		session.createSQLQuery(sql).setParameter("pname", pname).setParameter("cname", cname).executeUpdate();
		sql = "delete from vms.addctrl where pname = :pname and cname = :cname";
		session.createSQLQuery(sql).setParameter("pname", pname).setParameter("cname", cname).executeUpdate();
		session.close();
	}
	
	public String addActivity(String pname, String cname){
		AddCtrl addCtrl = new AddCtrl();
		addCtrl.setPname(pname);
		addCtrl.setCname(cname);
		String hql = "from AddCtrl where pname = '"+pname+"' and cname='"+cname+"'";
		if(getHibernateTemplate().find(hql).size() == 0){
			return getHibernateTemplate().save(addCtrl).toString();
		}
		else
			return "";
	}
}
